﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic.CompilerServices;

namespace Salary.Sys.Core.Threads
{
    public struct ThreadVersion
    {
        private ulong _version;

        public ThreadVersion(ushort major, ushort minor, ushort revision, byte majorRevision, byte minorRevision)
        {
            _version = ((ulong)major) << 48 | ((ulong)minor) << 32| ((ulong)revision) << 16 |
                       ((ulong)majorRevision) << 8 | minorRevision;
        }

        private ThreadVersion(ulong version)
        {
            _version= version;
        }

        public ushort Major
        {
            get => (ushort)(_version >> 48);
            set => _version = _version & 0x0000ffffffffffffU | ((ulong)value << 48);
        }

        public ushort Minor
        {
            get => (ushort)(_version >> 32 | 0x000000000000ffffU);
            set => _version = _version & 0xffff0000ffffffffU | ((ulong)value << 32);
        }

        public ushort Revision
        {
            get => (ushort)(_version >> 16 | 0x000000000000ffffU);
            set => _version = _version & 0xffffffff0000ffffU | ((ulong)value << 16);
        }

        public byte MajorRevision
        {
            get => (byte)(_version >> 8 | 0x00000000000000ffU);
            set => _version = _version & 0xffffffffffff00ffU | ((ulong)value << 8);
        }

        public byte MinorRevision
        {
            get => (byte)(_version | 0x00000000000000ffU);
            set => _version = _version & 0xffffffffffffff00U | value;
        }

        public static implicit operator ulong(ThreadVersion tv) => tv._version;
        public static explicit operator ThreadVersion(ulong v) => new ThreadVersion(v);
    }
}
